from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
import master.login as login
import master.utility as utility
import master.nodok as nodok

import tenantRel.business as business
import tenantRel.tarif as tarif
import tenantRel.tenantType as tenantType
import tenantRel.tenant as tenant
import tenantRel.unitType as  unitType
import tenantRel.tarifDet as  tarifDet
import tenantRel.unit as unit

import accounting.jurnalType as jurnalType
import accounting.coa as coa
import accounting.mapCode as mapCode
import accounting.cashFlow as cashFlow
import accounting.bankAcc as bankAcc
import accounting.jurnal as jurnal

import billing.meter as meter
import billing.billingSfsc as billingSfsc
import billing.billingUtil as billingUtil

import hashlib
import Ui_mainapp

class MainApp(QMainWindow, Ui_mainapp.Ui_mainApp):  
  def __init__(self, parent=None):
        super(MainApp, self).__init__(parent)
        self.setupUi(self)        
        self.db = QSqlDatabase.addDatabase("QMYSQL")
#        self.db = QSqlDatabase.addDatabase("QODBC")
        self.connectDb()
        self.mdi = QWorkspace()
        self.setCentralWidget(self.mdi)
        self.showMaximized()
        self.formDokNo  = None
        
        self.formBusiness = None
        self.formTarif = None
        self.formTenantType = None
        self.formTenant = None
        self.formUnitType = None
        self.formTarifDet = None
        self.formUnit = None
        
        self.formBankAcc = None
        self.formJournalType = None
        self.formMappingCode = None
        self.formCashFlowCode = None
        self.formAccountCode = None
        self.formJurnal = None
        
        self.formMeter = None
        self.formBillingSfsc = None
        self.formBillingUtil = None
        
        self.dataGlob = {"kode":"", "nama":"", "grup":"", "level":""}        
        
        
  def connectDb(self):
        self.db.setDatabaseName("matosdev")
        self.db = utility.Utility(self.db).setDBParm()
        self.db.setHostName("localhost")
        self.db.setPort(3306)
        if not self.db.open():
            QMessageBox.warning(None, "ConnectDB", QString("Database Error: %1").arg(self.db.lastError().text()))

  def actLogOut(self):
      self.dataGlob["kode"] = ""
      self.dataGlob["nama"] = ""
      self.dataGlob["grup"] = ""
      self.dataGlob["level"] = ""
      formlogin = login.Login(self)
      if formlogin.exec_():
          KODE_USER,  NAMA,  GRUP_MENU,  LEVEL_USER = range(4)
          nama = formlogin.userNameEdt.text()
          upass = formlogin.pwdEdt.text()    
          m = hashlib.md5()                 
          m.update(upass)
          query = QSqlQuery(self.db)
          queryStr = "select kode_user, nama, level, auth_level from master_user where kode_user = '"+nama+"' and pwd = '"+upass+"' and status = 'A'"
#          queryStr = "select kode_user, nama, level, auth_level from master_user where kode_user = '"+nama+"' and pwd = '"+m.hexdigest()+"' and status = 'A'"
          if self.db.isOpen():
              query.exec_(queryStr)
# ================== START - ini kalo pake driver mysql =============================
              if query.size() > 0:
                  while query.next():
                      self.dataGlob["kode"] = query.value(KODE_USER).toString()
                      self.dataGlob["nama"] = query.value(NAMA).toString()
                      self.dataGlob["grup"] = query.value(GRUP_MENU).toString()
                      self.dataGlob["level"] = query.value(LEVEL_USER).toString()
                  self.authApp()
                  formlogin.close()
              else:
                  QMessageBox.warning(None, "Login Error", "Wrong User Name / Password!")      
# ================== END - ini kalo pake driver mysql =============================

# ================== START - ini kalo pake driver odbc =============================
#              while query.next():
#                  self.dataGlob["kode"] = query.value(KODE_USER).toString()
#                  self.dataGlob["nama"] = query.value(NAMA).toString()
#                  self.dataGlob["grup"] = query.value(GRUP_MENU).toString()
#                  self.dataGlob["level"] = query.value(LEVEL_USER).toString()
#              self.authApp()
# ================== END - ini kalo pake driver odbc =============================

              if  self.dataGlob["kode"] == "":
                  QMessageBox.warning(None, "Login Error", "Wrong User Name / Password!")      
                  self.close()  
              else:
                  formlogin.close()
                
      else:
          self.close()    
          
#========= FILE =========    
  def actPrintSetup(self):
      printer = QPrinter()
      pageDlg = QPageSetupDialog(printer,  self)
      pageDlg.exec_()
      
#========= Tenancy =========            
  def actUnit(self):
      if self.formUnit is None:
          self.formUnit = unit.Unit(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formUnit)
      self.formUnit.showMaximized()
      self.formUnit.show()
      
  def actBusiness(self):
      if self.formBusiness is None:
          self.formBusiness = business.Business(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formBusiness)
      self.formBusiness.showMaximized()
      self.formBusiness.show()
      
  def actTarif(self):
      if self.formTarif is None:
          self.formTarif = tarif.Tarif(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formTarif)
      self.formTarif.showMaximized()
      self.formTarif.show()
      
  def actTenantType(self):
      if self.formTenantType is None:
          self.formTenantType = tenantType.TenantType(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formTenantType)
      self.formTenantType.showMaximized()
      self.formTenantType.show()
      
  def actTenant(self):
      if self.formTenant is None:
          self.formTenant = tenant.Tenant(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formTenant)
      self.formTenant.showMaximized()
      self.formTenant.show()
      
  def actUnitType(self):
      if self.formUnitType is None:
          self.formUnitType = unitType.UnitType(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formUnitType)
      self.formUnitType.showMaximized()
      self.formUnitType.show()
      
  def actTarifDet(self):
      if self.formTarifDet is None:
          self.formTarifDet = tarifDet.TarifDet(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formTarifDet)
      self.formTarifDet.showMaximized()
      self.formTarifDet.show()
      
#========= Billing =========    
  def actMeter(self):
      if self.formMeter is None:
          self.formMeter = meter.Meter(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formMeter)
      self.formMeter.showMaximized()
      self.formMeter.show()
      
  def actBillSfsc(self):
      if self.formBillingSfsc is None:
          self.formBillingSfsc = billingSfsc.BillingSfsc(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formBillingSfsc)
      self.formBillingSfsc.showMaximized()
      self.formBillingSfsc.show()
      
  def actBillUtil(self):
      if self.formBillingUtil is None:
          self.formBillingUtil = billingUtil.BillingUtil(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formBillingUtil)
      self.formBillingUtil.showMaximized()
      self.formBillingUtil.show()
      
#========= Cash Bank =========    

#========= Accounting =========    
  def actJurnal(self):
      if self.formJurnal is None:
          self.formJurnal = jurnal.Jurnal(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formJurnal)
      self.formJurnal.showMaximized()
      self.formJurnal.show()

  def actJournalType(self):
      if self.formJournalType is None:
          self.formJournalType = jurnalType.JurnalType(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formJournalType)
      self.formJournalType.showMaximized()
      self.formJournalType.show()

  def actAccountCode(self):
      if self.formAccountCode is None:
          self.formAccountCode = coa.Coa(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formAccountCode)
      self.formAccountCode.showMaximized()
      self.formAccountCode.show()

  def actMappingCode(self):
      if self.formMappingCode is None:
          self.formMappingCode = mapCode.MapCode(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formMappingCode)
      self.formMappingCode.showMaximized()
      self.formMappingCode.show()

  def actCashFlowCode(self):
      if self.formCashFlowCode is None:
          self.formCashFlowCode = cashFlow.CashFlow(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formCashFlowCode)
      self.formCashFlowCode.showMaximized()
      self.formCashFlowCode.show()

  def actBankAcc(self):
      if self.formBankAcc is None:
          self.formBankAcc = bankAcc.BankAcc(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formBankAcc)
      self.formBankAcc.showMaximized()
      self.formBankAcc.show()
#========= Purchasing =========    

#========= Logistic =========    

#========= Payroll =========    

#========= Admin =========    
  def actDokNo(self):
      if self.formDokNo is None:
          self.formDokNo = nodok.Nodok(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formDokNo)
      self.formDokNo.showMaximized()
      self.formDokNo.show()
      
      
  def authApp(self):
      print ""
#      self.actionStore.setEnabled(False)
#      self.actionCredit_Card_No.setEnabled(False)
#      self.actionDocument_No.setEnabled(False)
#      self.actionUser.setEnabled(False)
#      if self.dataGlob["level"] == "ADM":
#          self.actionUser.setEnabled(True)
#          self.actionStore.setEnabled(True)
#          self.actionCredit_Card_No.setEnabled(True)
#          self.actionDocument_No.setEnabled(True)
          
if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    form = MainApp()
#========= FILE =========    
    form.connect(form.actionExit, SIGNAL("triggered()"), form.close)
    form.connect(form.actionPrinter_Setup, SIGNAL("triggered()"), form.actPrintSetup)
    
#========= Tenancy =========    
    form.connect(form.actionUnit, SIGNAL("triggered()"), form.actUnit)
    form.connect(form.actionBusiness_Unit, SIGNAL("triggered()"), form.actBusiness)
    form.connect(form.actionTarif, SIGNAL("triggered()"), form.actTarif)
    form.connect(form.actionTenant_Type, SIGNAL("triggered()"), form.actTenantType)
    form.connect(form.actionTenant_Owner, SIGNAL("triggered()"), form.actTenant)
    form.connect(form.actionUnit_Type, SIGNAL("triggered()"), form.actUnitType)
    form.connect(form.actionTarif_Detail, SIGNAL("triggered()"), form.actTarifDet)
    
#========= Billing =========    
    form.connect(form.actionInput_Meteran, SIGNAL("triggered()"), form.actMeter)
    form.connect(form.actionBilling_SFSC, SIGNAL("triggered()"), form.actBillSfsc)
    form.connect(form.actionBilling_Utilities, SIGNAL("triggered()"), form.actBillUtil)
    
#========= Cash Bank =========    
#========= Accounting =========    
    form.connect(form.actionJournal_Type, SIGNAL("triggered()"), form.actJournalType)
    form.connect(form.actionAccount_Code, SIGNAL("triggered()"), form.actAccountCode)
    form.connect(form.actionMapping_Code, SIGNAL("triggered()"), form.actMappingCode)
    form.connect(form.actionCash_Bank_Account, SIGNAL("triggered()"), form.actBankAcc)
    form.connect(form.actionCash_Flow_Code, SIGNAL("triggered()"), form.actCashFlowCode)
    form.connect(form.actionAccount_Code, SIGNAL("triggered()"), form.actAccountCode)
    form.connect(form.actionJournal, SIGNAL("triggered()"), form.actJurnal)
    
#========= Purchasing =========    
#========= Logistic =========    
#========= Payroll =========    
#========= Admin =========    
    form.connect(form.actionMaster_No_Dok, SIGNAL("triggered()"), form.actDokNo)
    
    form.show()
    form.actLogOut()
    sys.exit(app.exec_())

